// pages/member/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickname:"",
    avatar:"",
    openid:"",
    tel:"",
    ent_id:"2",
    kefu_name:"kefu2",
    acount_openid:"",
  },
  getPhoneNumber (e) {
    var _this=this;
    console.log(e)
    var code=e.detail.code;
    if(!code){
      wx.showModal({
        title: e.detail.errMsg,
      });
      return;
    }

    wx.request({
      url: app.globalData.apiUrl+'/wechat/miniUserTel',
      data: {
        code: code,
        ent_id:_this.data.ent_id
      },
      success:function(res){
        console.log(res);
        var info=JSON.parse(res.data.result);
        var tel=info.phone_info.phoneNumber;
        if(!tel){
          return;
        }
        _this.setData({
          "tel":tel,
        });
      }
    });
  },
  saveCustomer(){
    var _this=this;
    wx.request({
      url: app.globalData.apiUrl+'/2/customerInfo',
      method:"POST",
      data: {
        ent_id:_this.data.ent_id,
        kefu_name:_this.data.kefu_name,
        nickname:_this.data.nickname,
        avatar:_this.data.avatar,
        tel:_this.data.tel,
        openid:_this.data.openid,
        acount_openid:_this.data.acount_openid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
      success:function(res){
        console.log(res);
        wx.showToast({
          title: '注册成功！',
        })
        wx.redirectTo({
          url: '/pages/index/index?ent_id='+_this.data.ent_id+"&kefu_name="+_this.data.kefu_name,
        })
      }
    })
  },

  syncInputValue: function(e){
    // console.log("输入：" + e.detail.value)
    this._tempInputValue = e.detail.value;
    this.setData({
      tel:e.detail.value
    });
  },
  setInputValue: function(e){
    let value = this._tempInputValue;
     console.log(e)
    if(!value) {return;}
    let isObj = e.target.dataset.isObj;
    let objName = e.target.dataset.objName;
    let fieldName = e.target.dataset.fieldName;
    if(isObj=="1"){
      let obj = this.data[`${objName}`];
      obj[fieldName] = this._tempInputValue;
      this.setData({
        [`${objName}`]: obj
      });
    }else{
      this.setData({
        [`${fieldName}`]: value
      })
    }
    this._tempInputValue = null;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      nickname:options.nickname,
      avatar:options.avatar,
      openid:options.openid,
      ent_id:options.ent_id,
      kefu_name:options.kefu_name,
      acount_openid:options.acount_openid,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})